using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Web.Configuration;
using System.Xml;

namespace NimblePros.SimpleCms
{
    public class CmsSettings
    {
        private static CmsSettings _instance;

        #region Constructors and Methods

        private CmsSettings()
        {
        }
        
        private CmsSettings(XmlNode section)
        {
            XmlNode CmsConfigContent = section.SelectSingleNode(@"/" + Resources.ConfigSection);

            if (CmsConfigContent != null)
            {

                if (CmsConfigContent.Attributes["connectionStringName"] != null)
                {
                    this.ConnectionStringName = CmsConfigContent.Attributes["connectionStringName"].InnerText;
                }
                else
                {
                    throw new CmsConfigException(new Exception(), ExceptionType.ConnectionStringName);
                }

                if (CmsConfigContent.Attributes["masterPage"] != null)
                {
                    this.MasterPage = CmsConfigContent.Attributes["masterPage"].InnerText;
                }
                else
                {
                    throw new CmsConfigException(new Exception(), ExceptionType.ConfigMasterPage);
                }

                if (CmsConfigContent.Attributes["adminPage"] != null)
                {
                    this.AdminPage = CmsConfigContent.Attributes["adminPage"].InnerText;
                }
                else
                {
                    throw new CmsConfigException(new Exception(), ExceptionType.AdminPage);
                }

                if (CmsConfigContent.Attributes["editPage"] != null)
                {
                    this.EditPage = CmsConfigContent.Attributes["editPage"].InnerText;
                }
                else
                {
                    throw new CmsConfigException(new Exception(), ExceptionType.EditPage);
                }

                if (CmsConfigContent.Attributes["defaultFileExtension"] != null)
                {
                    this.DefaultFileExtension = CmsConfigContent.Attributes["defaultFileExtension"].InnerText;
                }
                else
                {
                    throw new CmsConfigException(new Exception("Default File Extension not specified in Cms Configuration"), ExceptionType.Base);
                }

                if (ConfigurationManager.ConnectionStrings[ConnectionStringName] != null)
                {
                    this.ConnectionString = ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString;
                }
                else
                {
                    throw new CmsConfigException(new Exception(), ExceptionType.ConnectionString);
                }

                if (CmsConfigContent.Attributes["theme"] != null)
                {
                    this.Theme = CmsConfigContent.Attributes["theme"].InnerText;
                }
                else
                {
                    //This is an optional Setting so do nothing
                    this.Theme = string.Empty;
                }
            }
            else
            {
                throw new CmsConfigException(new Exception("CMS Config Section Not Specified."), ExceptionType.Base);
            }
        }

        public static CmsSettings getSettings(XmlNode section)
        {
            if (_instance == null)
            {
                _instance = new CmsSettings(section);
            }
            return _instance;
        }

        #endregion

        #region Properties

        private static string _connectionString;
        private string _connectionStringName;
        private string _masterPage;
        private string _adminPage;
        private string _editPage;
        private string _defaultFileExtension;
        private string _theme;

        public string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }

        public static string StaticConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }

        public string ConnectionStringName
        {
            get { return _connectionStringName; }
            set { _connectionStringName = value; }
        }

        public string MasterPage
        {
            get { return _masterPage; }
            set { _masterPage = value; }
        }

        public string AdminPage
        {
            get { return _adminPage; }
            set { _adminPage = value; }
        }

        public string EditPage
        {
            get { return _editPage; }
            set { _editPage = value; }
        }

        public string DefaultFileExtension
        {
            get { return _defaultFileExtension; }
            set { _defaultFileExtension = value; }
        }

        public string Theme
        {
            get { return _theme; }
            set { _theme = value; }
        }

        #endregion
    }
}
